package com.ruoyi.projects.apis;

import com.ruoyi.common.annotation.LoginUserInfo;

import com.ruoyi.common.constant.BuConstants;
import com.ruoyi.common.constant.WxAliConstants;
import com.ruoyi.common.core.controller.ApiBaseController;
import com.ruoyi.common.core.domain.ApiResult;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.projects.Vos.WxUserInfoDTO;
import com.ruoyi.projects.domain.SlApointForm;

import com.ruoyi.projects.domain.SlSchedules;
import com.ruoyi.projects.dto.SlApointFormVo;

import com.ruoyi.projects.service.ISlApointFormService;

import com.ruoyi.projects.service.ISlSchedulesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 店铺产品相关接口
 */
@RestController
@RequestMapping("/api/appointment")
public class ApointApis extends ApiBaseController {

    private static final Logger log = LoggerFactory.getLogger(ApointApis.class);

    @Autowired
    private ISlApointFormService slApointFormService;

    @Autowired
    private ISlSchedulesService slSchedulesService;

    /**
     * 小程序预约操作
     */
    @PostMapping("/appoint")
    public ApiResult appoint(@LoginUserInfo WxUserInfoDTO dto, @RequestBody SlApointFormVo vo)
    {
        SlApointForm slApointForm=new SlApointForm();
        slApointForm.setApointTime(vo.getApointTime());
        SlSchedules slSchedules=null;
        if (StringUtils.isEmpty(vo.getProductId())){
            //排期会员卡预约
            slSchedules = slSchedulesService.selectSlSchedulesBySId(vo.getsId());
            if (slSchedules != null){
                slApointForm.setProductId(slSchedules.getProductIds());
            }else {
                return ApiResult.error(ApiResult.EnumCode.ERROR,"排期数据无效");
            }
        }else {
            slApointForm.setProductId(vo.getProductId());
        }
        slApointForm.setCustomId(dto.getId());
        slApointForm.setApointType(vo.getApointType());
        slApointForm.setTimes(vo.getTimes());
        String timeSlot = vo.getTimeSlot();
        String[] timeParts = timeSlot.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            Date startTime = sdf.parse(timeParts[0]);
            Date endTime = sdf.parse(timeParts[1]);
            slApointForm.setCustomType(2);
            slApointForm.setStartHour(startTime);
            slApointForm.setEndHour(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int i = slApointFormService.insertSlApointForm(slApointForm);
        if (i>0){
            if (slSchedules!=null){
                //会员卡预约，需要更改一下排期的状态
                slSchedules.setPushState(BuConstants.FLAG_YES);
                slSchedulesService.updateSlSchedules(slSchedules);
            }
            return ApiResult.success();
        }else {
         return ApiResult.error("预约失败");
        }
    }

    /**
     * 查询产品预约时间段
     */
    @PostMapping("/queryAppointments")
    public ApiResult queryAppointments(@LoginUserInfo WxUserInfoDTO dto, @RequestBody SlApointFormVo vo)
    {
        SlApointForm slApointForm=new SlApointForm();
        slApointForm.setApointTime(vo.getApointTime());
        slApointForm.setCustomId(dto.getId());
        List<String> bookedTimeSlots=slApointFormService.queryAppointMents(slApointForm);
        return ApiResult.success(bookedTimeSlots);
    }

    /**
     * 查询产品预约时间段
     */
    @PostMapping("/queryCalendarApoints")
    public ApiResult queryCalendarApoints(@LoginUserInfo WxUserInfoDTO dto,@RequestBody SlApointFormVo vo)
    {
        SlApointForm slApointForm=new SlApointForm();
        slApointForm.setApointTime(vo.getApointTime());
        if (dto!=null){
            slApointForm.setCustomId(dto.getId());
            return ApiResult.success(slApointFormService.queryCalendarApoints(slApointForm));
        }else {
            return ApiResult.success(new ArrayList<>());
        }
    }


}






















